package com.example.xianji.activity;

import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.Request.Method;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.JsonObjectRequest;
import com.example.xianji.Contant;
import com.example.xianji.XJApplication;
import com.example.xianji.R;
import com.example.xianji.constant.Constant;
import com.example.xianji.utils.sp.PreferenceEngine;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class Main_InformationActivity extends Activity {
    /************个人 基本信息页面******************************/
//    private SharedPreferences sp;
//    private Editor ed;
    private RelativeLayout geren_jiben_xinxis_back, geren_jiben_xinxi_relative_delete;
    private TextView textview_nickname, textview_name, textview_sex, textview_place, textview_email, textview_mobile, textview_qq, textview_birthday, textview_guding_phone, bianji;
    private String province_name, city_name, district_name, code, tel, ext;
    private int geren_province_id, geren_city_id, geren_district_id;
    private boolean isopen;

    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        setContentView(R.layout.activity_main__information);
//        sp = getSharedPreferences("text", MODE_PRIVATE);
//        ed = sp.edit();

        //XJApplication.getInstance().addActivity(this);
        textview_nickname = (TextView) findViewById(R.id.textview_nickname);
        textview_name = (TextView) findViewById(R.id.textview_name);
        textview_sex = (TextView) findViewById(R.id.textview_sex);
        textview_place = (TextView) findViewById(R.id.textview_place);
        textview_email = (TextView) findViewById(R.id.textview_email);
        textview_mobile = (TextView) findViewById(R.id.textview_mobile);
        textview_qq = (TextView) findViewById(R.id.textview_qq);
        textview_birthday = (TextView) findViewById(R.id.textview_birthday);
        textview_guding_phone = (TextView) findViewById(R.id.textview_guding_phone);
        bianji = (TextView) findViewById(R.id.bianji);
        geren_jiben_xinxis_back = (RelativeLayout) findViewById(R.id.geren_jiben_xinxi_relative_back);
        geren_jiben_xinxi_relative_delete = (RelativeLayout) findViewById(R.id.geren_jiben_xinxi_relative_delete);
        get_information_data();
        geren_jiben_xinxis_back.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Main_InformationActivity.this.setResult(Contant.back_resultCode);
                Main_InformationActivity.this.finish();
            }
        });

        bianji.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Main_InformationActivity.this, Main_BianJi_XinxiActivity.class);
                intent.putExtra("nickname", textview_nickname.getText().toString());
                intent.putExtra("name", textview_name.getText().toString());
                intent.putExtra("sex", textview_sex.getText().toString());
                intent.putExtra("birthday", textview_birthday.getText().toString());
                intent.putExtra("province_name", province_name);
                intent.putExtra("city_name", city_name);
                intent.putExtra("district_name", district_name);
                intent.putExtra("geren_province_id", geren_province_id);
                intent.putExtra("geren_city_id", geren_city_id);
                intent.putExtra("geren_district_id", geren_district_id);
                intent.putExtra("phone", textview_mobile.getText().toString());
                intent.putExtra("email", textview_email.getText().toString());
                intent.putExtra("code", code);
                intent.putExtra("tel", tel);
                intent.putExtra("ext", ext);
                intent.putExtra("isopen", isopen);
                intent.putExtra("isMobile", Contant.has_mobile);
                intent.putExtra("QQ", textview_qq.getText().toString());
                startActivityForResult(intent, Contant.requestCode);
            }
        });
    }

    public void get_information_data() {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("token", PreferenceEngine.getInstance().getString(Constant.SP.NAME, Constant.SP.KEY_XIANJI_TOKEN, ""));
//        map.put("token", sp.getString("xianji_token", ""));
        JSONObject parmas = new JSONObject(map);

        JsonObjectRequest request = new JsonObjectRequest(Method.POST, Contant.yonghu_infor,
                parmas, new Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    if (response.getInt("status") == 1) {
                        JSONArray array = response.getJSONArray("data");
                        for (int i = 0; i < array.length(); i++) {
                            JSONObject object = array.getJSONObject(0);
                            JSONObject object1 = array.getJSONObject(1);
                            textview_nickname.setText(object.getString("nickname"));
                            textview_name.setText(object1.getString("name"));
                            if (object1.getString("sex").equals("Y")) {
                                textview_sex.setText("男");
                            } else if (object1.getString("sex").equals("N")) {
                                textview_sex.setText("女");
                            }
                            province_name = object1.getString("province_name");
                            city_name = object1.getString("city_name");
                            district_name = object1.getString("district_name");

                            geren_province_id = object1.getInt("province");
                            geren_city_id = object1.getInt("city");
                            geren_district_id = object1.getInt("district");
                            if (object1.getInt("isopen") == 0) {
                                isopen = false;
                            } else {
                                isopen = true;
                            }
                            code = object1.getString("code");
                            tel = object1.getString("tel");
                            ext = object1.getString("ext");

                            textview_place.setText(object1.getString("province_name") + object1.getString("city_name") + object1.getString("district_name"));
                            textview_email.setText(object.getString("email"));
                            textview_mobile.setText(object.getString("mobile"));
                            textview_qq.setText(object1.getString("qq"));
                            if (object1.getString("birth").equals("0000-00-00")) {
                                textview_birthday.setText("");
                            } else {
                                textview_birthday.setText(object1.getString("birth"));
                            }
                            if (object1.getString("ext").equals("0")) {
                                textview_guding_phone.setText(object1.getString("code") + object1.getString("tel"));
                            } else {
                                textview_guding_phone.setText(object1.getString("code") + object1.getString("tel") + object1.getString("ext"));
                            }
                        }
                    } else {
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
            }
        });
        XJApplication.getHttpQueues().cancelAll("abcPost");
        request.setTag("abcPost");
        request.setRetryPolicy(new DefaultRetryPolicy(50 * 1000, 1, 1.0f));
        XJApplication.getHttpQueues().add(request);
        XJApplication.getHttpQueues().start();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (Main_BianJi_XinxiActivity.geren_save == 1 ||
                PreferenceEngine.getInstance().getInt(Constant.SP.NAME, Constant.SP.KEY_CHANGE_PHONE, 0) == 1) {
            get_information_data();
        }
        Main_BianJi_XinxiActivity.geren_save = 0;
        PreferenceEngine.getInstance().putInt(Constant.SP.NAME, Constant.SP.KEY_CHANGE_PHONE, 0);
//        ed.putInt("change_phone", 0);
//        ed.commit();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Contant.requestCode && resultCode == Contant.back_resultCode) {
            geren_jiben_xinxi_relative_delete.setVisibility(View.VISIBLE);
            geren_jiben_xinxi_relative_delete.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Main_InformationActivity.this.setResult(Contant.delete_resultCode);
                    Main_InformationActivity.this.finish();
                }
            });
        }
    }
}
